package com.whz.work.ex02;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandler;

/**
 * @ClassName ServletProcessor1
 * @Description TODO
 * @Author hong-zhi
 * @Date 2021/5/28 21:00
 * @Version 1.0
 */
public class ServletProcessor2 implements Processor {
    @Override
    public void process(Request request, Response response) {
        String uri = request.getUri();
        String servletName = uri.substring(uri.lastIndexOf("/") + 1);
        URLClassLoader loader = null;

        URL[] urls = new URL[1];
        URLStreamHandler streamHandler = null;

        File classPath = new File(Constants.JAVA_PATH + File.separator + "ex02" + File.separator + "servlet" + File.separator);

        ServletRequest requestFacade = new RequestFacade(request);
        ServletResponse responseFacade = new ResponseFacade(response);
        try {
            loader = new URLClassLoader(new URL[]{new URL("file", null, Constants.JAVA_PATH )});
            Class<Servlet> clazz = (Class<Servlet>) loader.loadClass("com.whz.work.ex02.servlet.PrimitiveServlet");
            Servlet servlet = clazz.newInstance();
            servlet.service(requestFacade, responseFacade);
        } catch (IOException | ServletException | InstantiationException
                | IllegalAccessException | ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
